package com.doctor.ui.consulting;

import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.doctor.comm.ConstConfig;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.R;

public class AddBookServiceFrag extends BaseFragment{

	/**
	 * 新增预约服务
	 */
	
	private Context context;
	private LinearLayout saveBtn,cancelBtn;
	private EditText hospitalDepartment,phone,specialNum,webSite;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context=getActivity();
		
		View view=inflater.inflate(R.layout.add_book_servcice, container, false);
		initiaView(view);
				
		return view;
	}
	
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}	
	
	
	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		hospitalDepartment=(EditText) view.findViewById(R.id.hospitalDepartment);
		phone=(EditText) view.findViewById(R.id.phone);
		specialNum=(EditText) view.findViewById(R.id.specialNum);
		webSite=(EditText) view.findViewById(R.id.webSite);
		
		saveBtn=(LinearLayout) view.findViewById(R.id.saveBtn);
		saveBtn.setOnClickListener(onClick);
		cancelBtn=(LinearLayout) view.findViewById(R.id.cancelBtn);
		cancelBtn.setOnClickListener(onClick);
	}
	
	
	
	private OnClickListener onClick=new OnClickListener() {	
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if (arg0==saveBtn) {
				String hospitalDepartment_txt=hospitalDepartment.getText().toString().trim();
				String phone_txt=phone.getText().toString().trim();
				String specialNum_txt=specialNum.getText().toString().trim();
				String webSite_txt=webSite.getText().toString().trim();
				
				if (hospitalDepartment_txt==null || "".equals(hospitalDepartment_txt.trim())) {
					Toast.makeText(context, "请填写医院(科室)！", Toast.LENGTH_SHORT).show();
					return;
				}
				ContentValues values=new ContentValues();
				values.put("_hospital_partment", hospitalDepartment_txt);
				if (phone_txt!=null && !"".equals(phone_txt)) {
					values.put("_phone", phone_txt);
				}
				if (specialNum_txt!=null && !"".equals(specialNum_txt)) {
					values.put("_special_num", specialNum_txt);
				}
				if (webSite_txt!=null && !"".equals(webSite_txt)) {
					values.put("_webSite", webSite_txt);
				}
				long row=DbOperator.getInstance(context).insertData(ConstConfig.HOSPITAL_TABLE, values);
				if (row>0) {
					Toast.makeText(context, "保存成功！", Toast.LENGTH_SHORT).show();
					getActivity().getSupportFragmentManager().popBackStack();
				} else {
					Toast.makeText(context, "保存失败！", Toast.LENGTH_SHORT).show();
					return;
				}
				
			} else if (arg0==cancelBtn) {
				getActivity().getSupportFragmentManager().popBackStack();
			}
		}
	};
	
	
	
	
	
	
}
